package com.txc.openfeign.common.pay.utils;


import com.txc.openfeign.common.pay.dto.RcvDet;
import com.txc.openfeign.common.pay.vo.NotifyFileDetail;
import com.txc.openfeign.common.pay.vo.NotifyFileVo;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 生活缴费通知文件解析工具类
 *
 * @ClassName: NotificationFileParsUtile
 * @Author: TXC
 * @Date: 2023-01-31 09:50
 **/
public class NotificationFileParsUtil {
    public static NotifyFileVo analysisNotifyFile(InputStream inputStream) throws IOException {
        if (inputStream == null) {
            return null;
        }
        NotifyFileVo res = new NotifyFileVo();
        Reader reader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line;
        int lineNum = 0;
        while ((line = bufferedReader.readLine()) != null) {
            if (lineNum == 0) {
                List<String> lineList = Arrays.asList(line.split("\\|"));
                res.setTotalPaymentAmount(lineList.get(0));
                res.setTotalPaymentNum(lineList.get(1));
            }else {
                NotifyFileDetail notifyFileDetail = analysisNotifyFileDetail(line);
                if (res.getDetailList() == null) {
                    res.setDetailList(new ArrayList<>());
                }
                res.getDetailList().add(notifyFileDetail);
            }
            lineNum++;
        }
        return res;
    }

    public static NotifyFileDetail analysisNotifyFileDetail(String detail) {
        NotifyFileDetail notifyFileDetail = new NotifyFileDetail();
        List<String> lineList = Arrays.asList(detail.split("\\|"));
        notifyFileDetail.setConsNo(lineList.get(0));
        notifyFileDetail.setBankSerial(lineList.get(1));
        notifyFileDetail.setBankDate(lineList.get(2));
        notifyFileDetail.setRcvAmt(lineList.get(3));
        notifyFileDetail.setChargeCnt(lineList.get(4));
        List<String> rcvDetList = Arrays.asList(lineList.get(5).split("\\$"));
        RcvDet rcvDet = new RcvDet();
        rcvDet.setConsNo(rcvDetList.get(0));
        rcvDet.setRcvblAmtId(rcvDetList.get(1));
        rcvDet.setRcvblYm(rcvDetList.get(2));
        notifyFileDetail.setRcvDet(rcvDet);
        return notifyFileDetail;
    }

}
